<?php
namespace app\api\service;
use think\Controller;
use think\Db;
use think\Exception;
use think\Session;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/26 0026
 * Time: 15:40
 */

class Friend extends Controller {

    /**
     * 用户发起约酒交友活动
     * @param $param
     * @return mixed
     */
    public static function send_activity($param) {
        try {
            $validate_activity = validate('Activity');
            if (!$validate_activity->check($param)) {
                throw new \Exception($validate_activity->getError());
            }
            $data = [
                'member_id' => $param['member_id'],
                'order_sn' => 'HD' . date('YmdHis') . rand(1000,9999),
                'name' => $param['name'],
                'start_time' => strtotime($param['start_time']),
                'population' => $param['population'],
                'price' => config('system.margin'),
                'address' => $param['address'],
                'lat' => $param['lat'],
                'lng' => $param['lng'],
                'is_aa' => $param['is_aa'],
                'is_group_chat' => $param['is_group_chat'] == 1 ? 1 : 2,
                'msg' => $param['msg'],
                'image_1' => $param['image_1'] . '-700',
                'image_2' => $param['image_2'] . '-700',
                'image_3' => $param['image_3'] . '-700',
            ];

            $model = model('activity');
            $model->save($data);

            $create_group = Rong::create_group(['id' => $model->id]);
            if ($create_group['code'] != 200) {
                dump($create_group);
                throw new \Exception('群聊创建失败');
            }

            return api_return(200, ['id' => $model->id]);
        } catch (\Exception $e) {
            return api_return(0, [], $e->getMessage());
        }
    }

    /**
     * 获取可申请赞助列表
     * @param $param
     * @return array
     */
    public static function get_sponsor_list($param) {
        try {
            // 获取活动数据
            $activity = model('activity')->get(['id' => $param['id'], 'member_id' => $param['member_id']]);
            if (!$activity) {
                throw new \Exception('活动不存在');
            }

            if ($activity->status != 1) {
                throw new \Exception('活动当前状态不可申请赞助！');
            }

            // 根据活动经纬度获取附近店铺
            $coord = getAround($activity['lat'], $activity['lng'], 10000);
            $where['lat'] = ['between', [$coord[0], $coord[1]]];
            $where['lng'] = ['between', [$coord[2], $coord[3]]];
            $where['status'] = 2;
            $shop_ids = model('shop')->where($where)->column('id');

            // 根据店铺ID获取可用赞助商品
            $sponsors = model('shop_product')->alias('sp')
                ->field('p.name as p_name, p.price, p.sponsor_num, s.name as s_name, s.logo, sp.inventory, sp.product_id, sp.shop_id')
                ->join('jx_product p', 'p.id = sp.product_id', 'LEFT')
                ->join('jx_shop s', 's.id = sp.shop_id', 'LEFT')
                ->where('sp.shop_id', 'in', $shop_ids)
                ->where('p.is_sponsor', 1)
                ->where('p.sponsor_num', '<', $activity->population)
                ->select();

            $data = [];
            foreach ($sponsors as $v) {
                $product_image = model('product_image')->field('name, thumb')->where('product_id', $v->product_id)->order('is_main desc')->select();
//                foreach ($product_image as $k => $pi) {
//                    $product_image[$k]['thumb'] = get_img_url($pi->thumb);
//                }
                $data[] = [
                    'logo' => get_img_url($v->logo),
                    'p_name' => $v->p_name,
                    's_name' => $v->s_name,
                    'price' => $v->price,
                    'product_id' => $v->product_id,
                    'shop_id' => $v->shop_id,
                    'sponsor_num' => $v->sponsor_num,
                    'inventory' => $v->inventory,
                    'product_image' => $product_image,
                ];
            }

            return api_return(200, $data);
        } catch (\Exception $e) {
            return api_return(0, [], $e->getMessage());
        }
    }

    /**
     * 申请赞助
     * @param $param
     * @return array
     */
    public static function apply_for_sponsor($param) {
        try {
            // 获取活动数据
            $activity = model('activity')->get(['id' => $param['id']]);
            if (!$activity) {
                throw new \Exception('活动不存在');
            }

            if ($activity->member_id != $param['member_id']) {
                throw new \Exception('活动发起者才能申请赞助');
            }

            if ($activity->status != 1) {
                throw new \Exception('活动当前状态不可申请赞助！');
            }

            if ($activity->product_id > 0 || $activity->shop_id > 0) {
                throw new \Exception('每个活动只能申请一次赞助！');
            }

            $data = [
                'product_id' => $param['product_id'],
                'shop_id' => $param['shop_id'],
            ];

            model('activity')->save($data, ['id' => $param['id']]);

            return api_return(200, []);
        } catch (\Exception $e) {
            return api_return(0, [], $e->getMessage());
        }
    }


}